package com.sarees.main.util;

import java.io.IOException;

import junit.framework.Assert;

import org.openqa.selenium.server.SeleniumServer;

import lib.System.SYS;
import lib.System.Utility.Report;
import lib.wrapper.SeleniumAb;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class WebUtil 
{
	SeleniumServer selServer;
	//Selenium selenium;
	SeleniumAb selenium = new SeleniumAb("localhost",4444,"*iexplore","http://www.onlinedesignersarees.com");
	public void checkAndStartServer() throws Exception
	{
		//check if the server is already started
		
		
		selServer = new SeleniumServer();
		//check the server
		try
		{
			selServer.stop();
		}
		catch(Exception e)
		{
			// check that server is stopped if running stop
			System.out.println("The server is already is stargted.");
			System.out.println(e.getMessage());
			
		}
		
		try
		{
			//start server at default port
			selServer.start();
		}
		catch(Exception e)
		{
			//if server started to failed exit by assert failure
			System.out.println("Server failed to start./n");Assert.fail(e.getMessage());
		}
		
	}
	
	public void chekAndStartSeleniumInstance() throws Exception
	{
		
			//check if the server is already started
					
			
			//check the server
			try
			{
				selenium.stop();
			}
			catch(Exception e)
			{
				// check that server is stopped if running stop
				System.out.println("The server is already is stargted.");
				System.out.println(e.getMessage());
				
			}
			
			try
			{
				//start server at default port
				selenium.start();
			}
			catch(Exception e)
			{
				//if server started to failed exit by assert failure
				System.out.println("Server failed to start./n");Assert.fail(e.getMessage());
			}
	}

	public void openHomePage() throws IOException 
	{
		// TODO Auto-generated method stub

		selenium.open("/");
		selenium.waitForPageToLoad(SYS.TIMEOUT);
		selenium.windowMaximize();
		//check the title of the home page
		if(selenium.getTitle().contains("Online Sarees"))
		{
			//The title is matching the title
			System.out.println("Home page opened as expected");
		}
		else
		{
			Report.failMSG("Home page not opend");
		}
	}
	
	public void stopServer()
	{
		selServer.stop();
	}
	public void stopServerInstance()
	{
		selenium.stop();
	}
	
	public SeleniumAb getSel()
	{
		return this.selenium;
	}
}
